#include "stdafx.h"
#include "Graphics.h"



CGraphics::CGraphics()
{
}


CGraphics::~CGraphics()
{
}


//setup pixel format - no need to change any of this usually
bool
CGraphics::bSetupPixelFormat(HDC hdc_)
{
	PIXELFORMATDESCRIPTOR pfd;
	int pixelformat;

	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);// Set the size of the structure
	pfd.nVersion = 1;					// Always set this to 1
	// Pass in the appropriate OpenGL flags
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.dwLayerMask = PFD_MAIN_PLANE;	// We want the standard mask (this is ignored anyway)
	pfd.iPixelType = PFD_TYPE_RGBA;	// We want RGB and Alpha pixel type
	pfd.cColorBits = 16;
	pfd.cDepthBits = 0;		// Depthbits is ignored for RGBA, but we do it anyway
	pfd.cAccumBits = 0;	// No special bitplanes needed
	pfd.cStencilBits = 0;	// We desire no stencil bits

	// This gets us a pixel format that best matches the one passed in from the device
	if ((pixelformat = ChoosePixelFormat(hdc_, &pfd)) == FALSE)
	{
		MessageBoxA(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
		return FALSE;
	}

	// This sets the pixel format that we extracted from above
	if (SetPixelFormat(hdc_, pixelformat, &pfd) == FALSE)
	{
		MessageBoxA(NULL, "SetPixelFormat failed", "Error", MB_OK);
		return FALSE;
	}

	return TRUE;
}


//resize OpenGL viewport and projection matrix to suit new window size
void 
CGraphics::SetWindowSize(int width_, int height_)
{
	glViewport(0, 0, width_, height_);	// reset the viewport to new dimensions
	glMatrixMode(GL_PROJECTION);	// set projection matrix current matrix
	glLoadIdentity();				// reset projection matrix
	// calculate aspect ratio of window
	gluPerspective(45.0f, (GLfloat)width_ / (GLfloat)height_, 1.0f, 1000.0f);
	glMatrixMode(GL_MODELVIEW);		// set modelview matrix
	glLoadIdentity();				// reset modelview matrix
}



//setup OpenGL rendering in specified Device Context
void
CGraphics::CreateGLWindow(HWND hwnd_)
{
	m_HDC = GetDC(hwnd_);
	bSetupPixelFormat(m_HDC); // call our pixel format setup function
	m_hRC = wglCreateContext(m_HDC); // create rendering context 
	wglMakeCurrent(m_HDC, m_hRC); //and make it current

	//load textures - not really the most logical place to do this, needs to be 'refactored' or moved into a more sensible place
	//CreateTexture("alien1.tga", 1); //load bitmap into memory
	//CTextureLoader::getInstance()->CreateTexture("assets/alien1.tga", 1);

}



// call this function when window is destroyed to free up memory
void
CGraphics::DestroyGLWindow(HWND hwnd_)
{
	wglMakeCurrent(NULL, NULL);	// This frees our rendering memory and sets //everything back to normal
	wglDeleteContext(m_hRC);	// Delete our OpenGL Rendering Context	

	if (m_HDC)
	{
		ReleaseDC(hwnd_, m_HDC);
	}
	m_HDC = NULL;
	m_hRC = NULL;

}

void
CGraphics::DrawTestSquare()
{
	//load textures
	//CreateTexture("alien1.tga", 1); //load bitmap into memory

	glBindTexture(GL_TEXTURE_2D, 1); //use the one and only texture!
	glEnable(GL_TEXTURE_2D);

	glEnable(GL_BLEND);// Turn Blending On
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	// clear screen and depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(0, 0, -5.0f);		// move back 5 units
	glColor3f(1.0f, 0.0f, 0.0f);		// set color to red

	// Display a quad 
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(-1, 1, 0);

	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-1, -1, 0);

	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(1, -1, 0);

	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(1, 1, 0);
	glEnd();
}


void 
CGraphics::ClearFrame()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

}

void
CGraphics::SwapGLBuffers()
{
	SwapBuffers(m_HDC);
}

//instantiate static variables and initialize them to null
HDC CGraphics::m_HDC = NULL;
HGLRC CGraphics::m_hRC = NULL;